#ifndef QUERYHISTORYMANAGER_H
#define QUERYHISTORYMANAGER_H

#include <QObject>
#include <QString>
#include <QList>
#include <QDateTime>
#include <QSqlDatabase>
#include <QSqlQuery>

struct QueryHistoryItem {
    int id;
    QString query;
    QDateTime timestamp;
    QString databaseName;
    
    QueryHistoryItem() : id(0) {}
    QueryHistoryItem(const QString &q, const QDateTime &t, const QString &dbName)
        : id(0), query(q), timestamp(t), databaseName(dbName) {}
};

class QueryHistoryManager : public QObject
{
    Q_OBJECT

public:
    explicit QueryHistoryManager(QObject *parent = nullptr);
    ~QueryHistoryManager();
    
    bool initialize(const QString &databasePath = QString());
    void addQuery(const QString &query, const QString &databaseName);
    QList<QueryHistoryItem> getRecentQueries(int count = 60) const;
    QList<QueryHistoryItem> searchQueries(const QString &searchTerm) const;
    bool clearHistory();
    int historyCount() const;
    
signals:
    void historyUpdated();

private:
    bool createTables();
    QString getDefaultDatabasePath() const;
    
    QSqlDatabase m_database;
    QString m_databasePath;
    bool m_initialized;
};

#endif // QUERYHISTORYMANAGER_H